package se.topshelf.golftracker.core.domain;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.Serializable;

/**
 *
 */
@Embeddable
public class Address implements Serializable {
    @NotEmpty
    private String street;
    @NotNull
    private String streetNumber;
    @NotEmpty
    @Length(min = 5, max = 7, message = "{address.postalCode.length}")
    @Pattern(regexp = "[0-9]+")
    private String postalCode;
    @NotEmpty
    private String postalAddress;

    @NotNull
    @ManyToOne()
    @JoinColumn(name = "countryId", updatable = false)
    private Country country;

    public Address() {
    }

    public Address(String street, String streetNumber, String postalCode, String postalAddress, Country country) {
        this.street = street;
        this.streetNumber = streetNumber;
        this.postalCode = postalCode;
        this.postalAddress = postalAddress;
        this.country = country;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getStreetNumber() {
        return streetNumber;
    }

    public void setStreetNumber(String streetNumber) {
        this.streetNumber = streetNumber;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public String getPostalAddress() {
        return postalAddress;
    }

    public void setPostalAddress(String postalAddress) {
        this.postalAddress = postalAddress;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Address address = (Address) o;

        if (!country.equals(address.country)) return false;
        if (!postalAddress.equals(address.postalAddress)) return false;
        if (!postalCode.equals(address.postalCode)) return false;
        if (!street.equals(address.street)) return false;
        if (!streetNumber.equals(address.streetNumber)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = street.hashCode();
        result = 31 * result + streetNumber.hashCode();
        result = 31 * result + postalCode.hashCode();
        result = 31 * result + postalAddress.hashCode();
        result = 31 * result + country.hashCode();
        return result;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Address");
        sb.append("{street='").append(street).append('\'');
        sb.append(", streetNumber='").append(streetNumber).append('\'');
        sb.append(", postalCode='").append(postalCode).append('\'');
        sb.append(", postalAddress='").append(postalAddress).append('\'');
        sb.append(", country=").append(country);
        sb.append('}');
        return sb.toString();
    }
}
